package com.xzjie.core.jedis;

import redis.clients.jedis.JedisCluster;

/**
 * jedis cluster manager
 * 
 * @author xzjie
 *
 */
public class JedisManager {
	
	private JedisCluster jedisCluster;

	/**
	 * The value is obtained by key
	 * 
	 * @param key
	 * @return The value of key in redis
	 * @throws Exception
	 */
	public byte[] getValueByKey(byte[] key) throws Exception {
		byte[] result = null;
		try {
			result = jedisCluster.get(key);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	/**
	 * Delete the value in the redis key
	 * @param key
	 * @throws Exception
	 */
	public void deleteByKey(byte[] key) throws Exception {
		try {
			jedisCluster.del(key);
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * Save the value
	 * @param key
	 * @param value
	 * @param expireTime
	 * @throws Exception
	 */
	public void saveValueByKey(byte[] key, byte[] value, int expireTime) throws Exception {
		try {
			jedisCluster.set(key, value);
			if (expireTime > 0)
				jedisCluster.expire(key, expireTime);
		} catch (Exception e) {
			throw e;
		}
	}

	public JedisCluster getJedisCluster() {
		return jedisCluster;
	}

	public void setJedisCluster(JedisCluster jedisCluster) {
		this.jedisCluster = jedisCluster;
	}
}
